
import os
import re
import threading
import time

import requests

class Get_Comic():

    def __init__(self):
        self.homepage = 'https://www.lieqiman.com'  # 网页链接
        self.path = os.getcwd()  # 获取当前文件夹目录
        self.header = {
            'User-Agent': 'Mozilla/5.0 (Macintosh:Intel Mac OS X 10_11_4) Applewebkit/537.36 (KHTML, likeGecko)'}
        self.path_jpg = ''  # 设置全局变量,为存储漫画图片路径

    def get_home(self):  # 获取首页
        r = requests.get(self.homepage + '/mh/xe/index.html',
                         headers=self.header)
        r.encoding = 'GBK'  # 申明编码,否则中文乱码
        pages = re.findall(
            '<a href="(/mh/xe/index_\d.html)">\d</a>', r.text, re.S)
        #self.get_storys(url=self.homepage + '/mh/xe/index.html')  # 修改下载页面节点
        for page in pages:
            self.get_storys(url=self.homepage + page)

    def get_storys(self, url=''):  # 获取所有故事列表和标题
        r = requests.get(url, headers=self.header)
        r.encoding = 'GBK'  # 申明编码,否则中文乱码
        storys = re.findall(
            '<a href="(/mh/xe/\d+.html)" target="_blank" title="(.*?)">', r.text, re.S)
        for story in storys:
            self.get_chapters(url_c=self.homepage + story[0], name_c=story[1])

    def get_chapters(self, url_c='', name_c=''):  # url为故事链接,name为故事名称
        print(name_c)
        print(url_c)
        r = requests.get(url_c, headers=self.header)
        r.encoding = 'GBK'  # 申明编码,否则中文乱码
        chapters = re.findall(
            '<a class="album_link2" href="(/mhzj/\d+.html)" title="(.*?)" target="_blank">', r.text, re.S)
        # print(chapters)
        for chapter in chapters:
            # 注意这里的chapter[1]中包含敏感符号,如?等不可用于文件名的字符
            chapter_ = re.sub('[/:*?"<>|]', '', chapter[1])
            self.path_jpg = self.path+'/漫画下载/'+name_c
            if not os.path.exists(self.path_jpg):  # 判断文件夹是否存在,否 则创建
                os.makedirs(self.path_jpg)  # 本函数变量包括故事名称和章节名称,故在本函数建立图片存储路径
            self.path_jpg = self.path_jpg+'/'+chapter_
            print(self.path_jpg)
            try:
                self.get_pictures(url_p=self.homepage +
                                chapter[0], name_p=chapter[1])
            except:
                print('运行报错:'+chapter[1])

    def get_pictures(self, url_p='', name_p=''):
        r = requests.get(url_p, headers=self.header)
        r.encoding = 'GBK'  # 申明编码,否则中文乱码
        # print(url_p)
        # print(r.text) #注意,若正则无法匹配,请在本步骤查看网页源码,判断源码和浏览器显示是否一致
        pictures = re.findall(
            '<img src="(https://pic.lieqiman.com/xe/.*?.jpg)" />', r.text, re.S)
        i = 1  # 图片编号用
        for picture in pictures:
            # print(picture)
            t = threading.Thread(target=self.download,
                                 args=(picture, i))  # 多线程,提高下载效率
            t.start()
            i = i + 1

    def download(self, d_picture='', d_i=1):
        jpg = self.path_jpg + str(d_i) + '.jpg'
        r = requests.get(d_picture, headers=self.header)
        with open(jpg, 'wb') as f:
            f.write(r.content)

    def main(self):
        self.get_home()


if __name__ == "__main__":
    gc = Get_Comic()
    gc.main()
